<?php

class Db_Tables2_BContract
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function LaunchSwitcher($contract_id, $launch_switcher=1, $position=0)
	{
		$result = FALSE;
        $mod_mail = new Db_Tables2_BMailList();
		
		//add log start
		$mod_logs = new Db_Tables2_BLogsContractLaunch();
		
		try {
				$mod_logs->AddRecord($_SESSION['identity']['userid'], $_SESSION['identity']['realname'], $_SESSION['identity']['level'], $contract_id, $launch_switcher, $position);
			} catch (Exception $e) {
				
			}
		//add log end

        if($contract_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_contract", array("launch_switcher"));
			$select->where("contract_id = ?", $contract_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				if($row['launch_switcher'] != $launch_switcher)
				{
					$set = array("launch_switcher" => $launch_switcher);
					
					switch ($launch_switcher)
					{
						case 0:
							$set['status'] = 5; //暂停
							break;
						case 1:
							$set['status'] = 3; //等待执行主管确认
							break;
						case 2:
							$set['status'] = 4; //完成
							break;
					}
					
					$where = "contract_id = ".$contract_id;
					
					if($this->dealers_db->update("b_contract", $set, $where))
					{
						//写入审批日志
						$mod_logs_approval = new Db_Tables2_BLogsContractApproval();
						$mod_logs_approval->AddRecord($contract_id, $_SESSION['identity']['userid'], $_SESSION['identity']['realname'], $set['status']);
						
						if(1 == $launch_switcher)
						{
							//cancel mail confirmation
							$set = array("mail_confirm_time" => NULL);
							$where = "contract_id = '".$contract_id."'";
							$this->dealers_db->update("b_contract_sku", $set, $where);
							
							//send mail
							$mod_mail->SendServiceNotification($contract_id);

						}elseif(0 == $launch_switcher)
						{
							//cancel mail confirmation
							$set = array("mail_confirm_time" => NULL);
							$where = "contract_id = '".$contract_id."'";
							$this->dealers_db->update("b_contract_sku", $set, $where);
						}
                        $mod_mail->SendServiceNotificationToSales($contract_id);

                        //update result
						$result = TRUE;
					}
				}
			}
		}
		
		return $result;
	}
	
	function UpdateContractValue($contract_id, $opt_type, $value) //opt_type 1=plus 2=minus
	{
		$result = FALSE;
		
		if($contract_id && $opt_type && $value)
		{
			$contents ="";
			
			$select = $this->dealers_db->select();
			$select->from("b_contract", array("received_value", "launch_switcher"));
			$select->where("contract_id = ?", $contract_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$original_value = $row['received_value'];
				$received_value = $row['received_value'];
				
				if(1 == $opt_type) //加
				{
					$received_value += $value;
					$contents = "认款金额增加".$value."元，原金额".$original_value."元，新金额".$received_value."元";
				}elseif(2 == $opt_type) //减
				{
					$received_value -= $value;
					
					if($received_value < 0)
					{
						$received_value = 0;
					}

					$contents = "认款金额减去".$value."元，原金额".$original_value."元，新金额".$received_value."元";
				}
				
				$set = array("received_value" => $received_value);
				
				$where = "contract_id=".$contract_id;
				
				if($this->dealers_db->update("b_contract", $set, $where))
				{
					//add action log
					$mod_log = new Db_Tables2_BLogsAction();
					$mod_log->action_id = 1;
					$mod_log->related_id = $contract_id;
					$mod_log->contents = $contents;
					if($mod_log->addRecord())
					{
						$result = TRUE;
					}
				}
			}
		}
		
		return $result;
	}
	
	/**
	 * 逻辑上并不严密，一个合同里面有多个SKU，判断是否续约应该是按SKU判断
	 * 但考虑到系统中许多地方把他放在合同属性上，这里就按整个合同判断，只要合同里有一个SKU出现在30天之内，就算续约
	 * Mark 2018.03.20
	 */
	function isRenew($contract_id) //FALSE 新签 TRUE 续签 
	{
		$result = FALSE;
		
		if($contract_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_contract", array("client_id", "start_date"));
			$select->where("contract_id = ?", $contract_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				if($row['client_id'] && $row['start_date'])
				{
					$client_id = $row['client_id'];
					$start_date = $row['start_date'];
					
					//get sku
					$select = $this->dealers_db->select();
					$select->from("b_contract_sku", array("sku_id"));
					$select->where("contract_id = ?", $contract_id);
					$rows = $this->dealers_db->fetchAll($select);
					
					$sku_arr = array();
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$sku_arr[] = $row['sku_id'];
						}
					}
					
					if(!empty($sku_arr))
					{
						$target_date = date("Y-m-d", mktime(0,0,0,substr($start_date,5,2),substr($start_date,8,2)-30,substr($start_date,0,4)));

						$select = $this->dealers_db->select();
						$select->from("b_contract", array("contract_id"));
						$select->where("client_id = ?", $client_id);
						$select->where("expired_date >= ?", $target_date);
						$rows = $this->dealers_db->fetchRow($select);

						$target_contract_id_arr = array();
						$target_sku_arr = array();

						if(!empty($rows))
						{
							foreach($rows as $row)
							{
								$target_contract_id_arr[] = $row['contract_id'];
							}
						}
						
						if(!empty($target_contract_id_arr))
						{
							$select = $this->dealers_db->select();
							$select->from("b_contract_sku", array("sku_id"));
							$select->where("contract_id IN (?)", $target_contract_id_arr);
							$rows = $this->dealers_db->fetchAll($select);
							
							if(!empty($rows))
							{
								foreach($rows as $row)
								{
									$target_sku_arr[] = $row['sku_id'];
								}
							}
						}
						
						if(!empty(array_intersect($sku_arr, $target_sku_arr)))
						{
							$result = TRUE;
						}
					}
				}
			}
		}
		
		return $result;
	}

    /**
     * 修改合同签约日期
     * @param $contract_id
     * @param $sign_date
     */
	function UpdateContractSignDate($contract_id, $sign_date){
        $set = array("sign_date" => $sign_date);
        $where = "contract_id=".$contract_id;
        if($this->dealers_db->update("b_contract", $set, $where))
        {
            //add action log
            $mod_log = new Db_Tables2_BLogsAction();
            $mod_log->action_id = 2;//修改签约日期
            $mod_log->related_id = $contract_id;
            $mod_log->contents = "修改合同签约日期：".$sign_date;
            if($mod_log->addRecord())
            {
                $result = TRUE;
            }
        }
    }
}